package com.hzc.springchan.service.impl.business;

import com.hzc.springchan.bean.entity.UserEntity;
import com.hzc.springchan.bean.exception.CException;
import com.hzc.springchan.bean.request.Request;
import com.hzc.springchan.bean.response.MapDataResponse;
import com.hzc.springchan.constant.System;
import com.hzc.springchan.enums.PrivError;
import com.hzc.springchan.enums.PubError;
import com.hzc.springchan.enums.impl.EmsBusiness;
import com.hzc.springchan.mapper.UserMapper;
import com.hzc.springchan.service.IBusinessService;
import com.hzc.springchan.service.IVerifyCodeService;
import com.hzc.springchan.util.JSONUtils;
import com.hzc.springchan.util.LogUtils;
import com.hzc.springchan.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 注册业务处理类
 * @author chen990627
 * @date 2022/10/19
 */
@Service(System.ApiBusinessName.USER_REGISTER)
public class UserRegisterBusiness implements IBusinessService<Request, MapDataResponse> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    @Qualifier(System.VerifyCodeBean.EMS)
    private IVerifyCodeService emsVerifyCodeService;

    @Override
    public void doBusiness(Request request, MapDataResponse response) {

        //这里用户名暂时只指邮箱号
        String userName = (String) request.get("userName");
        String passWord = (String) request.get("passWord");
        String nickName = (String) request.get("nickName");

        request.put("receiver", userName);
        request.put("business", EmsBusiness.USER_REGISTER);

        //验证码校验
        IVerifyCodeService.VerifyResponse verifyResponse = emsVerifyCodeService.verifyCode(request);
        if (!verifyResponse.isSuccess()) {
            LogUtils.info(this, "验证码校验失败：{}", verifyResponse.getResultCode());
            response.setError(PubError.VERIFY_CODE_FAILED);
            return;
        }

        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("userName", userName);
        paramMap.put("userStatus", 0);
        List<UserEntity> userEntityList = userMapper.findByOption(paramMap);
        if (!CollectionUtils.isEmpty(userEntityList)) {
            LogUtils.info(this, "用户{}已存在", userName);
            response.setFailure(PubError.REGISTER_USERNAME_ERROR, PrivError.REGISTER_USERNAME_ERROR);
            return;
        }

        try {
            MD5Utils md5Utils = new MD5Utils();
            String newPassWord = md5Utils.encodeByMd5(passWord);

            UserEntity userEntity = new UserEntity();
            userEntity.setUserEmail(userName);
            userEntity.setPassWord(newPassWord);
            userEntity.setNickName(nickName);
            userMapper.save(userEntity);

            if (userEntity.getId() == null) {
                response.setFailure();
                LogUtils.error("用户注册失败: {}", JSONUtils.toJSON(userEntity));
                throw new CException(PrivError.REGISTER_ERROR);
            }
        } catch (NoSuchAlgorithmException e) {
            LogUtils.info(this, "No such algorithm exception!");
        } catch (UnsupportedEncodingException e) {
            LogUtils.info(this,"Unsupported encoding exception!");
        }

        emsVerifyCodeService.deleteCode(request);
    }
}
